package AdminController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.DictIsNotFoundException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.renjun.smweb.controller.AdminController.AdminController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by huamin on 2017/2/23.
 * 用户管理M4
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class AdminControllerTestCase {

    @Autowired
    private AdminController adminController;

    //模拟request,response
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginInfo());
//        response = new MockHttpServletResponse();
    }

    private LoginInfo getLoginInfo() {
        LoginInfo loginInfo = new LoginInfo();
  //      loginInfo.setId("测试人员");
        loginInfo.setId("b9a1c118-d92a-4190-8177-f99e58221fb2");
        loginInfo.setRoleid("2");
        loginInfo.setDistrictcode("310116");
        loginInfo.setRoletype("2");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }

    /**
     * 登陆M4-9
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testLogin(){
        System.out.println("M4-9登陆");
        //封装参数
        Map<String,Object> params=new HashMap<>();
    }

    /**
     * 用户列表M4-1
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testAdminUserList(){
        System.out.println("M4-1用户列表");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("limit","10");
        params.put("offset","0");
        params.put("order","");
        params.put("sort","");
        params.put("districtcode","310116");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //用户列表M4-1
        JSONObject rs = adminController.adminUserList(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 用户列表导出M4-11
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testExpAdminUserList(){
        System.out.println("M4-1用户列表导出");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("offset","0");
        params.put("order","");
        params.put("sort","");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //用户列表M4-1
        JSONObject rs = adminController.expAdminUserList(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 查看用户信息M4-5
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testShowAdminUser(){
        System.out.println("M4-5查看用户信息");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("id","110a2911-5b50-4bda-aae9-f3e0138a2c94");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //查看用户信息M4-5
        JSONObject rs = adminController.showAdminUser(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 用户修改信息M4-3
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testUpdateAdminUser(){
        System.out.println("M4-3用户修改信息");
        //封装参数
        Map<String,Object> params=new HashMap<>();
   //     params.put("id","110a2911-5b50-4bda-aae9-f3e0138a2c94");
   //     params.put("realname","haha");
        validateUpdateSameSfzjh(params);  //重复身份证件号
    //    validateUpdateSameMobile(params);   //重复联系电话
    //    validateUpdateSameEmail(params);    //重复邮箱
    //    validateUpdateRealname(params);    //校验姓名，必须为中文
    //    validateUpdateMobile(params);   //校验联系电话，11位首位不为0
     //   validateUpdateEmail(params);    //校验邮箱，符合邮箱规则
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //用户修改信息M4-3
        JSONObject rs = adminController.updateAdminUser(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    //校验重复身份证件号
    private void validateUpdateSameSfzjh(Map<String, Object> params){
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("realname","测试新增用户");
        params.put("sfzjh","1234567890");
        params.put("mobile","12345678888");
        params.put("email","123@123.com");
        params.put("districtcode","310116");
        params.put("schoolcode","3101160002");
        params.put("roleid","3");
    }

    //校验重复联系电话
    private void validateUpdateSameMobile(Map<String, Object> params){
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("realname","测试新增用户");
        params.put("sfzjh","310110199911111111");
        params.put("mobile","13333333333");
        params.put("email","123@123.com");
        params.put("districtcode","310116");
        params.put("schoolcode","3101160002");
        params.put("roleid","3");
    }

    //校验重复邮箱
    private void validateUpdateSameEmail(Map<String, Object> params){
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("realname","测试新增用户");
        params.put("sfzjh","310110199911111111");
        params.put("mobile","12345678888");
        params.put("email","456@456.com");
        params.put("districtcode","310116");
        params.put("schoolcode","3101160002");
        params.put("roleid","3");
    }

    //校验姓名合法性
    private void validateUpdateRealname(Map<String, Object> params){
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("realname","6fa");
        params.put("sfzjh","310110199911111111");
        params.put("mobile","12345678");
        params.put("email","123");
        params.put("districtcode","310116");
        params.put("schoolcode","3101160002");
        params.put("roleid","3");
    }

    //校验联系电话合法性
    private void validateUpdateMobile(Map<String, Object> params){
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("realname","测试新增用户");
        params.put("sfzjh","310110199911111111");
        params.put("mobile","43478");
        params.put("email","123");
        params.put("districtcode","310116");
        params.put("schoolcode","3101160002");
        params.put("roleid","3");
    }

    //校验邮箱合法性
    private void validateUpdateEmail(Map<String, Object> params){
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("realname","测试新增用户");
        params.put("sfzjh","310110199911111111");
        params.put("mobile","12345678");
        params.put("email","123dd#");
        params.put("districtcode","310116");
        params.put("schoolcode","3101160002");
        params.put("roleid","3");
    }

    /**
     * 新增用户M4-2
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testAddAdminUser(){
        System.out.println("M4-2新增用户");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        validateAddSameEname(params);  //用户名是否重复
   //     validateAddSameSfzjh(params);   //身份证是否重复
    //    validateAddSameMobile(params);  //联系电话是否重复
    //    validateAddSameEmail(params);    //邮箱是否重复
    //    validateAddEname(params);  //校验用户名是否为字母数字组合，首位必须为字母，否抛出异常
     //   validateAddRealname(params);   //校验姓名，必须为中文
    //    validateAddMobile(params);   //校验联系电话，11位首位不为0
    //    validateAddEmail(params);     //校验邮箱，符合邮箱规则
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //新增用户M4-2
        JSONObject rs = adminController.addAdminUser(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    //用户名是否重复
    private void validateAddSameEname(Map<String, Object> params){
        params.put("ename","0010");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","18888888888");
        params.put("email","111123@123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //身份证是否重复
    private void validateAddSameSfzjh(Map<String, Object> params){
        params.put("ename","d0010");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","1234567890");
        params.put("mobile","18888888888");
        params.put("email","111123@123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //联系电话是否重复
    private void validateAddSameMobile(Map<String, Object> params){
        params.put("ename","d0010");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","13131331313");
        params.put("email","111123@123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //邮箱是否重复
    private void validateAddSameEmail(Map<String, Object> params){
        params.put("ename","d0010");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","18888888888");
        params.put("email","11112@11.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //校验用户名是否为字母数字组合，首位必须为字母，否抛出异常
    private void validateAddEname(Map<String, Object> params){
        params.put("ename","4gjs鱼");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","18888888888");
        params.put("email","111123@123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //校验姓名，必须为中文
    private void validateAddRealname(Map<String, Object> params){
        params.put("ename","d0010");
        params.put("realname","");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","18888888888");
        params.put("email","111123@123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //校验联系电话，11位首位不为0
    private void validateAddMobile(Map<String, Object> params){
        params.put("ename","d0010");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","vabvsh");
        params.put("email","111123@123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    //校验邮箱，符合邮箱规则
    private void validateAddEmail(Map<String, Object> params){
        params.put("ename","d0010");
        params.put("realname","测试新增用户哈");
        params.put("sfzjh","310110199911111118");
        params.put("mobile","18888888888");
        params.put("email","111123123.com");
        params.put("roleid","3");
        params.put("districtcode","310116");
        params.put("schoolcode","0002");
        params.put("roletype","3");
    }

    /**
     * 用户注销M4-4
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testDelAdminUser(){
        System.out.println("M4-4用户注销");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //用户注销M4-4
        JSONObject rs = adminController.delAdminUser(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 用户恢复M4-12
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testRegainAdminUser(){
        System.out.println("M4-12用户恢复");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //用户注销M4-4
        JSONObject rs = adminController.regainAdminUser(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 清除用户管理员邮箱M4-13
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testClearUserEmail(){
        System.out.println("M4-13清除用户管理员邮箱");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //清除用户管理员邮箱M4-13
        JSONObject rs = null;
        try {
            rs = adminController.clearUserEmail(request, response);
        } catch (DictIsNotFoundException e) {
            e.printStackTrace();
        }
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 重置用户管理员密码M4-14
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testRecoverUserPwd(){
        System.out.println("M4-14重置用户管理员密码");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //重置用户管理员密码M4-14
        JSONObject rs = null;
        try {
            rs = adminController.recoverUserPwd(request, response);
        } catch (DictIsNotFoundException e) {
            e.printStackTrace();
        }
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 修改用户管理员密码M4-8
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testModifyAdminPwd(){
        System.out.println("M4-8修改用户管理员密码");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("id","b9a1c118-d92a-4190-8177-f99e58221fb2");
        params.put("oldpwd","123456");
        params.put("newpwd","654321");
        params.put("adminid","b9a1c118-d92a-4190-8177-f99e58221fb2");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //修改用户管理员密码M4-8
        JSONObject rs = null;
        try {
            rs = adminController.modifyAdminPwd(request, response);
        } catch (DictIsNotFoundException e) {
            e.printStackTrace();
        }
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" + Encrypt.jiem(jiem)+ "\n");
    }

    //校验旧密码是否正确
    private void validateOldPassword(Map<String, Object> params) {
        params.put("adminid", "b05709e5-4b3b-4ae8-8e10-73c3018dbc17");
        params.put("oldpwd", "qweqwe");
        params.put("newpwd", "Ceshi123");
        }

    //校验新旧密码是否相同
    private void validateOldNewSamePassword(Map<String, Object> params) {
        params.put("adminid", "b05709e5-4b3b-4ae8-8e10-73c3018dbc17");
        params.put("oldpwd", "ceshi123");
        params.put("newpwd", "ceshi123");
    }

    //校验新密码是否符合规则
    private void validateNewPassword(Map<String, Object> params) {
        params.put("adminid", "b05709e5-4b3b-4ae8-8e10-73c3018dbc17");
        params.put("oldpwd", "ceshi123");
        params.put("newpwd", "haha111");
    }

}
